package cn.com.hp.feature.controller;

import cn.com.hp.feature.domain.dto.CommunityAndMenuParamDTO;
import cn.com.hp.feature.domain.dto.CommunityInfoDTO;
import cn.com.hp.feature.domain.dto.MenuInfoDTO;
import cn.com.hp.feature.domain.entity.CommunityInfo;
import cn.com.hp.feature.domain.entity.CommunityMenu;
import cn.com.hp.feature.domain.entity.MenuInfo;
import cn.com.hp.feature.domain.vo.CommunityMenuVO;
import cn.com.hp.feature.domain.vo.PageVO;
import cn.com.hp.feature.exception.BussinessException;
import cn.com.hp.feature.response.BaseResponse;
import cn.com.hp.feature.response.BaseResponseStatus;
import cn.com.hp.feature.service.ICommunityInfoService;
import cn.com.hp.feature.service.ICommunityMenuService;
import cn.com.hp.feature.service.IMenuInfoService;
import cn.com.hp.feature.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: hjz
 * @since: 2022/8/29
 * @description:
 */
@RestController
@AllArgsConstructor
@RequestMapping("/community")
@Api(value = "社区管理", tags = "社区管理")
public class CommunityInfoController {
    @Autowired
    private ICommunityInfoService communityInfoService;
    @Autowired
    private ICommunityMenuService communityMenuService;

    /**
     * 详情
     */
    @GetMapping("/detail/{communId}")
    @ApiOperation(value = "根据id获取详情", notes = "传入社区的id")
    public BaseResponse<CommunityInfo> detail(@PathVariable String communId) {
        BaseResponse baseResponse = new BaseResponse(BaseResponseStatus.OK);
        CommunityInfo detail = communityInfoService.getById(communId);
        baseResponse.setData(detail);
        return baseResponse;
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增社区信息", notes = "communityInfo")
    public BaseResponse save(@RequestBody CommunityInfo communityInfo) {
        BaseResponse baseResponse = new BaseResponse();
        communityInfoService.save(communityInfo);
        baseResponse.setData(communityInfo);
        return baseResponse;
    }



    /**
     * 更新
     */
    @PostMapping("/update/by/{communId}")
    @ApiOperation(value = "更新社区信息", notes = "communityInfo")
    public BaseResponse update(@PathVariable String communId,@RequestBody CommunityInfo communityInfo) {
        BaseResponse baseResponse = new BaseResponse();
        communityInfo.setId(communId);
        communityInfoService.updateById(communityInfo);
        baseResponse.setData(communityInfo);
        return baseResponse;
    }

    /**
     * 根据条件查询列表信息list
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表查询", notes = "communityInfo")
    public BaseResponse list(CommunityInfo communityInfo) {
        BaseResponse baseResponse = new BaseResponse(BaseResponseStatus.OK);
        List<CommunityInfo> communityInfos = communityInfoService.selectCommunityList(communityInfo);
        baseResponse.setData(communityInfos);
        return baseResponse;
    }

    /**
     * 条件查询社区信息(分页)
     * @param communityInfoDTO
     * @return
     */
    @ApiOperation(value = "分页查询社区信息列表", notes = "分页查询社区信息列表")
    @ResponseBody
    @GetMapping("/page")
    public BaseResponse<PageVO<CommunityInfo>> selectCommunityPageByRequest(CommunityInfoDTO communityInfoDTO) {
        BaseResponse baseResponse = new BaseResponse(BaseResponseStatus.OK);
        PageVO<CommunityInfo> dataList = communityInfoService.selectCommunityPageByRequest(communityInfoDTO);
        baseResponse.setData(dataList);
        return baseResponse;
    }

    /**
     * 新增
     */
    @PostMapping("/menu/save/{communId}")
    @ApiOperation(value = "社区绑定菜单", notes = "communityInfo")
    public BaseResponse saveMenuCommunity(@PathVariable String communId,@RequestBody List<String> menuIds) {
        List<CommunityMenu> communityMenus = new ArrayList<>();
        if (ToolUtil.isEmpty(menuIds)){
            throw new BussinessException("400", "菜单不能为空");
        }
        for (String menuId:menuIds){
            CommunityMenu communityMenu = new CommunityMenu();
            communityMenu.setCommunityId(communId);
            communityMenu.setMenuId(menuId);
            communityMenus.add(communityMenu);
        }
        communityMenuService.saveBatch(communityMenus);
        return new BaseResponse();
    }

    /**
     * 根据条件查询社区菜单列表信息list
     */
    @GetMapping("/andMenu/list")
    @ApiOperation(value = "根据条件查询社区菜单列表信息list")
    public BaseResponse communityAndMenulist(CommunityAndMenuParamDTO param) {
        BaseResponse baseResponse = new BaseResponse(BaseResponseStatus.OK);
        List<CommunityMenuVO> communityMenuVOList = communityInfoService.communityAndMenulist(param);
        baseResponse.setData(communityMenuVOList);
        return baseResponse;
    }

    /**
     * 条件查询社区菜单(分页)
     * @param param
     * @return
     */
    @ApiOperation(value = "分页查询社区菜单列表", notes = "分页查询社区菜单列表")
    @ResponseBody
    @GetMapping("/andMenu/page")
    public BaseResponse<PageVO<CommunityMenuVO>> selectCommunityAndMenuPage(CommunityAndMenuParamDTO param) {
        BaseResponse baseResponse = new BaseResponse(BaseResponseStatus.OK);
        PageVO<CommunityMenuVO> dataList = communityInfoService.selectCommunityAndMenuPage(param);
        baseResponse.setData(dataList);
        return baseResponse;
    }

    /**
     * 批量删除社区数据
     * @param communtityIds
     * @return
     */
    @ApiOperation(value = "批量删除社区数据", notes = "批量删除社区数据")
    @ResponseBody
    @PostMapping("/delete")
    public BaseResponse deleteMenu(@RequestBody List<String> communtityIds) {
        communityInfoService.removeByIds(communtityIds);
        return new BaseResponse();
    }
}
